package zhang.lao.service.console;/**
 * Created by zhangqin on 2017/8/28.
 */

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.build.tool.date.DateStyle;
import zhang.lao.build.tool.date.DateUtil;
import zhang.lao.dao.base.*;
import zhang.lao.pojo.ApiGt06PointReq;
import zhang.lao.pojo.ApiGt06PointResp;
import zhang.lao.pojo.console.map.PointAutoMachineResp;
import zhang.lao.pojo.console.map.PointGt06Resp;
import zhang.lao.pojo.console.map.PointResp;
import zhang.lao.pojo.console.req.AbLineResp;
import zhang.lao.pojo.console.req.GetAutoMachineTraceReq;
import zhang.lao.service.search.SearchService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 坐标点
 *
 * @author
 * @create 2017-08-28 17:55
 **/
@Service
public class PointRespService {

    @Autowired
    private SearchService searchService;
    @Autowired
    private WnAbLineDao wnAbLineDao;
    @Autowired
    private WnMachineDeviceLinkDao wnMachineDeviceLinkDao;
    @Autowired
    private WnMachineDao wnMachineDao;
    @Autowired
    private MachineService machineService;
    @Autowired
    private WnLnglatAutomaticDao wnLnglatAutomaticDao;

    @Autowired
    private WnAppUserDao wnAppUserDao;

    /**
     * 获取监控设备信息
     * @param wnLnglatAutomaticNew
     * @return
     */
    public PointResp<PointGt06Resp> buildGt06PointResp(WnLnglatGt06New wnLnglatAutomaticNew){
        WnMachineDeviceLinkExample wnMachineDeviceLinkExample = new WnMachineDeviceLinkExample();
        wnMachineDeviceLinkExample.createCriteria().andDeviceIdEqualTo(wnLnglatAutomaticNew.getDeviceId());
        WnMachineDeviceLink wnMachineDeviceLink  = ListUtils.getFirst(wnMachineDeviceLinkDao.selectByExample(wnMachineDeviceLinkExample));
        String licensePlateNumber ="未关联农机";
        PointGt06Resp pointAutoMachineResp = new PointGt06Resp();
        if(wnMachineDeviceLink!=null){
            String machineId = wnMachineDeviceLink.getMachineId();
            WnMachine wnMachine = wnMachineDao.selectByPrimaryKey(machineId);
            licensePlateNumber = wnMachine.getLicensePlateNumber();

            WnMachineAppUser wnMachineAppUser = machineService.getMachineAppUser(wnMachine.getId());

            if(StringUtils.isNotBlank(wnMachineAppUser.getAppUserId())){
                WnAppUser wnAppUser = wnAppUserDao.selectByPrimaryKey(wnMachineAppUser.getAppUserId());
                if(wnAppUser!=null){
                    pointAutoMachineResp.setUserPhoneNumber(wnAppUser.getPhoneNumber());
                    pointAutoMachineResp.setUserName(wnAppUser.getNickName());
                }
            }
        }else{

        }
        PointResp<PointGt06Resp> pointAutoMachineRespPointResp = new PointResp<>();

        pointAutoMachineResp.setSpeed(String.valueOf(wnLnglatAutomaticNew.getSpeed()));
        pointAutoMachineResp.setTime(DateUtil.DateToString(wnLnglatAutomaticNew.getCreateTime(), DateStyle.YYYY_MM_DD_HH_MM));
        pointAutoMachineResp.setLicensePlateNumber(licensePlateNumber);
        pointAutoMachineResp.setDeviceId(wnLnglatAutomaticNew.getDeviceId());
        pointAutoMachineRespPointResp.setData(pointAutoMachineResp);
        pointAutoMachineRespPointResp.setLat(Double.parseDouble(wnLnglatAutomaticNew.getLat()));
        pointAutoMachineRespPointResp.setLng(Double.parseDouble(wnLnglatAutomaticNew.getLng()));
        if((new Date().getTime()-wnLnglatAutomaticNew.getCreateTime().getTime())/1000>30){
            pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/machine_stop.png@"+wnLnglatAutomaticNew.getOrentation());
        }
        else{
            if(wnLnglatAutomaticNew.getSpeed()>30){
                pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/machine_work_warn.png@"+wnLnglatAutomaticNew.getOrentation());
            }else {
                pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/machine_work.png@" + wnLnglatAutomaticNew.getOrentation());
            }
        }
        return pointAutoMachineRespPointResp;
    }

    /**
     * 获取监控设备信息
     * @param wnLnglatAutomaticNew
     * @return
     */
    public PointResp<PointAutoMachineResp> buildAutoMachine(WnLnglatAutomaticNew wnLnglatAutomaticNew){
        WnMachineDeviceLinkExample wnMachineDeviceLinkExample = new WnMachineDeviceLinkExample();
        wnMachineDeviceLinkExample.createCriteria().andDeviceIdEqualTo(wnLnglatAutomaticNew.getDeviceId());
        WnMachineDeviceLink wnMachineDeviceLink  = ListUtils.getFirst(wnMachineDeviceLinkDao.selectByExample(wnMachineDeviceLinkExample));
        String licensePlateNumber ="未关联农机";
        if(wnMachineDeviceLink!=null){
            String machineId = wnMachineDeviceLink.getMachineId();
            WnMachine wnMachine = wnMachineDao.selectByPrimaryKey(machineId);
            licensePlateNumber = wnMachine.getLicensePlateNumber();
        }
        PointResp<PointAutoMachineResp> pointAutoMachineRespPointResp = new PointResp<>();
        PointAutoMachineResp pointAutoMachineResp = new PointAutoMachineResp();
        pointAutoMachineResp.setBreadth(wnLnglatAutomaticNew.getWorkWidth());
        pointAutoMachineResp.setSpeed(String.valueOf(wnLnglatAutomaticNew.getSpeed()));
        pointAutoMachineResp.setTime(DateUtil.DateToString(wnLnglatAutomaticNew.getCreateTime(), DateStyle.YYYY_MM_DD_HH_MM));
        pointAutoMachineResp.setLicensePlateNumber(licensePlateNumber);
        pointAutoMachineResp.setDeviceId(wnLnglatAutomaticNew.getDeviceId());
        pointAutoMachineRespPointResp.setData(pointAutoMachineResp);
        pointAutoMachineRespPointResp.setLat(Double.parseDouble(wnLnglatAutomaticNew.getLat()));
        pointAutoMachineRespPointResp.setLng(Double.parseDouble(wnLnglatAutomaticNew.getLng()));
        if((new Date().getTime()-wnLnglatAutomaticNew.getCreateTime().getTime())/1000>30){
            pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/machine_stop.png@"+wnLnglatAutomaticNew.getOrentation());
        }
        else{
            if(wnLnglatAutomaticNew.getSpeed()>8){
                pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/machine_work_warn.png@"+wnLnglatAutomaticNew.getOrentation());
            }else {
                pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/machine_work.png@" + wnLnglatAutomaticNew.getOrentation());
            }
        }
        return pointAutoMachineRespPointResp;
    }

    /**
     * 获取GT06轨迹
     * @return
     */
    public List<PointResp> buildGt06Trace(GetAutoMachineTraceReq getAutoMachineTraceReq) throws Exception {
        int deviceId = getAutoMachineTraceReq.getDeviceId();
        WnLnglatGt06Example wnLnglatGt06Example = new WnLnglatGt06Example();
        WnLnglatGt06Example.Criteria criteria = wnLnglatGt06Example.createCriteria();
        wnLnglatGt06Example.setOrderByClause("create_time asc");
        criteria.andDeviceIdEqualTo(deviceId);
        if(StringUtils.isNotBlank(getAutoMachineTraceReq.getStartTime())){
            criteria.andCreateTimeGreaterThan(DateUtil.StringToDate(getAutoMachineTraceReq.getStartTime()));
        }
        if(StringUtils.isNotBlank(getAutoMachineTraceReq.getEndTime())){
            criteria.andCreateTimeLessThan(DateUtil.StringToDate(getAutoMachineTraceReq.getEndTime()));
        }
        ApiGt06PointReq apiGt06PointReq = new ApiGt06PointReq();
        apiGt06PointReq.setStartTime(getAutoMachineTraceReq.getStartTime());
        apiGt06PointReq.setEndTime(getAutoMachineTraceReq.getEndTime());
        apiGt06PointReq.setDeviceId(getAutoMachineTraceReq.getDeviceId()+"");
        List<ApiGt06PointResp> wnLnglatGt06s = searchService.gt06Resp(apiGt06PointReq);
        List<PointResp> list = new ArrayList<>();
        if(wnLnglatGt06s!=null){
            for (int i = 0; i < wnLnglatGt06s.size(); i++) {
                ApiGt06PointResp wnLnglatGt06 = wnLnglatGt06s.get(i);
                PointResp pointAutoMachineRespPointResp = new PointResp();
                pointAutoMachineRespPointResp.setLat(wnLnglatGt06.getLat());
                pointAutoMachineRespPointResp.setLng(wnLnglatGt06.getLng());
                pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/point.png@"+wnLnglatGt06.getOrentation());

                if(i==0){
                    pointAutoMachineRespPointResp.setIcon("/static/image/map/start.png");
                }else if(i==wnLnglatGt06s.size()-1){
                    pointAutoMachineRespPointResp.setIcon("/static/image/map/end.png");
                }
                /*else if ( i%10==0){
                    pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/point-1.png@"+wnLnglatGt06.getOrentation());
                }*/

                PointAutoMachineResp pointAutoMachineResp = new PointAutoMachineResp();
                pointAutoMachineResp.setTime(wnLnglatGt06.getCreateTime());
                pointAutoMachineRespPointResp.setData(pointAutoMachineResp);
                list.add(pointAutoMachineRespPointResp);
            }
        }
        return list;
    }

    /**
     * 获取设备轨迹
     * @return
     */
    public List<PointResp> buildAutoMachineTrace(GetAutoMachineTraceReq getAutoMachineTraceReq){
        int deviceId = getAutoMachineTraceReq.getDeviceId();
        WnLnglatAutomaticExample wnLnglatAutomaticExample = new WnLnglatAutomaticExample();
        WnLnglatAutomaticExample.Criteria criteria = wnLnglatAutomaticExample.createCriteria();
        wnLnglatAutomaticExample.setOrderByClause("create_time asc");
        criteria.andDeviceIdEqualTo(deviceId);
        if(StringUtils.isNotBlank(getAutoMachineTraceReq.getStartTime())){
            criteria.andCreateTimeGreaterThan(DateUtil.StringToDate(getAutoMachineTraceReq.getStartTime()));
        }
        if(StringUtils.isNotBlank(getAutoMachineTraceReq.getEndTime())){
            criteria.andCreateTimeLessThan(DateUtil.StringToDate(getAutoMachineTraceReq.getEndTime()));
        }
        List<WnLnglatAutomatic> wnLnglatAutomatics = wnLnglatAutomaticDao.selectByExample(wnLnglatAutomaticExample);
        List<PointResp> list = new ArrayList<>();
       if(wnLnglatAutomatics!=null){
           for (int i = 0; i < wnLnglatAutomatics.size(); i++) {
               WnLnglatAutomatic wnLnglatAutomatic = wnLnglatAutomatics.get(i);
               PointResp pointAutoMachineRespPointResp = new PointResp();
               pointAutoMachineRespPointResp.setLat(Double.parseDouble(wnLnglatAutomatic.getLat()));
               pointAutoMachineRespPointResp.setLng(Double.parseDouble(wnLnglatAutomatic.getLng()));
               pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/point.png@"+wnLnglatAutomatic.getOrentation());
               if(i==0){
                   pointAutoMachineRespPointResp.setIcon("/static/image/map/start.png");
               }else if(i==wnLnglatAutomatics.size()-1){
                   pointAutoMachineRespPointResp.setIcon("/static/image/map/end.png");
               }
               /*else if ( i%10==0){
                   pointAutoMachineRespPointResp.setIcon("/console/machine/monitoring/image/point-1.png@"+wnLnglatAutomatic.getOrentation());
               }*/

               PointAutoMachineResp pointAutoMachineResp = new PointAutoMachineResp();
               pointAutoMachineResp.setTime(DateUtil.DateToString(wnLnglatAutomatic.getCreateTime(), DateStyle.YYYY_MM_DD_HH_MM));
               pointAutoMachineRespPointResp.setData(pointAutoMachineResp);
               list.add(pointAutoMachineRespPointResp);
           }
       }
     return list;
    }

    /**
     * 获取ab线列表
     * @return
     */
    public List<AbLineResp> abLineRespList(){
        List<WnAbLine> wnAbLines = wnAbLineDao.selectByExample(null);
        List<AbLineResp> lineResps = new ArrayList<>();
        if(wnAbLines!=null){
            for (WnAbLine wnAbLine : wnAbLines) {
                AbLineResp abLineResp = new AbLineResp();
                abLineResp.setName(wnAbLine.getName());
                PointResp apointResp = new PointResp();
                apointResp.setLat(Double.parseDouble(wnAbLine.getaLat()));
                apointResp.setLng(Double.parseDouble(wnAbLine.getaLng()));
                apointResp.setIcon("/static/image/map/a_point.png");
                abLineResp.setApointResp(apointResp);
                PointResp bpointResp = new PointResp();
                bpointResp.setLat(Double.parseDouble(wnAbLine.getbLat()));
                bpointResp.setLng(Double.parseDouble(wnAbLine.getbLng()));
                bpointResp.setIcon("/static/image/map/b_point.png");
                abLineResp.setBpointResp(bpointResp);
                lineResps.add(abLineResp);
            }
        }
        return lineResps;
    }
}
